package ui.actions;

import java.awt.geom.Point2D;

import prefuse.Constants;
import prefuse.Visualization;
import prefuse.action.Action;
import prefuse.data.tuple.TupleSet;
import prefuse.visual.VisualItem;
import ui.JTreeView;

public class AutoPanAction extends Action {
    private Point2D m_start = new Point2D.Double();
    private Point2D m_end   = new Point2D.Double();
    private Point2D m_cur   = new Point2D.Double();
    private JTreeView tree;
    private int     m_bias  = 150;
    
    public AutoPanAction(JTreeView t){
    	super();
    	tree = t;
    	
    }
    
    public void run(double frac) {
        TupleSet ts = m_vis.getFocusGroup(Visualization.FOCUS_ITEMS);
        if ( ts.getTupleCount() == 0 )
            return;
        
        if ( frac == 0.0 ) {
            int xbias=0, ybias=0;
            switch ( tree.getOrientation()) {
            case Constants.ORIENT_LEFT_RIGHT:
                xbias = m_bias;
                break;
            case Constants.ORIENT_RIGHT_LEFT:
                xbias = -m_bias;
                break;
            case Constants.ORIENT_TOP_BOTTOM:
                ybias = m_bias;
                break;
            case Constants.ORIENT_BOTTOM_TOP:
                ybias = -m_bias;
                break;
            }

            VisualItem vi = (VisualItem)ts.tuples().next();
            m_cur.setLocation(tree.getWidth()/2, tree.getHeight()/2);
            tree.getAbsoluteCoordinate(m_cur, m_start);
            m_end.setLocation(vi.getX()+xbias, vi.getY()+ybias);
        } else {
            m_cur.setLocation(m_start.getX() + frac*(m_end.getX()-m_start.getX()),
                              m_start.getY() + frac*(m_end.getY()-m_start.getY()));
            tree.panToAbs(m_cur);
        }
    }

}